import wx
import ViewWishList
import ViewCollectList
import ViewTodo
import ViewRecord
import SimplePanels
import LogManager
from SyncManager import SyncManager
from wx.lib.pubsub import setuparg1
from wx.lib.pubsub import pub


from SimplePanels import PanelCollect, PanelTodo, PanelWish, PanelRecord

M_DATA_MODE_CHANGED = ("MAINFORM","DATA_MODE_CHANGED")

class MainForm(wx.Frame):
    """
    this class implement the top level panel on which the tool bar resieds
    """
    def __init__(self, db_name='no database'):
        wx.Frame.__init__(self, None, wx.ID_ANY, "day day up")

        #all the child will inheritate this font size
        font = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
        font.SetPointSize(12)
        self.SetTitle(db_name)
        self.SetFont(font)

        self.InitToolBar()
        self.InitViews()
        self.Maximize()
        self.Show()


    def InitToolBar(self):
        tb = self.toolbar = self.CreateToolBar()
        statusbar = self.statusbar = self.CreateStatusBar()
        
        btn1 = wx.NewId()
        btn2 = wx.NewId()
        btn3 = wx.NewId()
        btn4 = wx.NewId()
        btn5 = wx.NewId()
        btn6 = wx.NewId()
        btn7 = wx.NewId()
        
        tb.AddSimpleTool(btn1,wx.Bitmap('res/wishlist.png'),"wish list","")
        tb.AddSimpleTool(btn2,wx.Bitmap('res/collectlist.png'),"collect list","")
        tb.AddSimpleTool(btn3,wx.Bitmap('res/todolist.png'),"todo list","")
        tb.AddSimpleTool(btn4,wx.Bitmap('res/rating.png'),"rating","")
        tb.AddSimpleTool(btn5,wx.Bitmap('res/mood.png'),"mood","")
        tb.AddSimpleTool(btn6,wx.Bitmap('res/sync.png'),"syn","")
        tb.AddSimpleTool(btn7,wx.Bitmap('res/database.png'),"change database","")
        #if you do not have this, you will not see the toolbar on windows
        #the strange thing is that it works well on linux
        tb.AddSeparator()
        tb.AddControl(wx.StaticBitmap(tb, -1, wx.Bitmap('res/logo.png')))
        
        self.toolbar.Realize()

        self.Bind(wx.EVT_TOOL, self.OnToolbarButton1, id = btn1)
        self.Bind(wx.EVT_TOOL, self.OnToolbarButton2, id = btn2)
        self.Bind(wx.EVT_TOOL, self.OnToolbarButton3, id = btn3)
        self.Bind(wx.EVT_TOOL, self.OnToolbarButton4, id = btn4)
        self.Bind(wx.EVT_TOOL, self.OnToolbarButton5, id = btn5)
        self.Bind(wx.EVT_TOOL, self.OnToolbarButton6, id = btn6)
        self.Bind(wx.EVT_TOOL, self.OnToolbarButton7, id = btn7)

    def InitViews(self):
        self.views = []
        self.sizer_top = wx.BoxSizer(wx.HORIZONTAL)
        #sub1 contails main views, sub2 contains panels(actually
        #they are all panels)
        sizer_sub1 = wx.BoxSizer(wx.VERTICAL)
        sizer_sub2 = wx.BoxSizer(wx.VERTICAL)
        self.sizer_top.Add(sizer_sub1, 1, wx.EXPAND|wx.ALL)
        self.sizer_top.Add(sizer_sub2, 0, wx.EXPAND|wx.ALL)
        
        wlist = ViewWishList.ViewWishList(self)
        wlist.Hide()
        clist = ViewCollectList.ViewCollectList(self)
        clist.Hide()
        tview = ViewTodo.ViewTodo(self)
        tview.Hide()
        rview = ViewRecord.ViewRecord(self)

        wpanel = PanelWish(self)
        wpanel.Hide()
        cpanel = PanelCollect(self)
        cpanel.Hide()
        tpanel = PanelTodo(self)
        tpanel.Hide()
        rpanel = PanelRecord(self)
        #rpanel.Hide()
        
        self.views.append((wlist, wpanel))
        self.views.append((clist, cpanel))
        self.views.append((tview, tpanel))
        self.views.append((rview, rpanel))
        
        sizer_sub1.Add(wlist, 1, wx.EXPAND|wx.ALL)
        sizer_sub1.Add(clist, 1, wx.EXPAND|wx.ALL)
        sizer_sub1.Add(tview, 1, wx.EXPAND|wx.ALL)
        sizer_sub1.Add(rview, 1, wx.EXPAND|wx.ALL)

        sizer_sub2.Add(wpanel, 1, wx.EXPAND|wx.ALL)
        sizer_sub2.Add(cpanel, 1, wx.EXPAND|wx.ALL)
        sizer_sub2.Add(tpanel, 1, wx.EXPAND|wx.ALL)
        sizer_sub2.Add(rpanel, 1, wx.EXPAND|wx.ALL)

        self.SetSizer(self.sizer_top)

    def OnToolbarButton1(self, evt):
        self.show_new(0)
    def OnToolbarButton2(self, evt):
        self.show_new(1)
    def OnToolbarButton3(self, evt):
        self.show_new(2)

    def OnToolbarButton4(self, evt):
        #NOTE: button4 and button5 will activate the same view
        pub.sendMessage(M_DATA_MODE_CHANGED, data='R')
        self.show_new(3)
        
    def OnToolbarButton5(self, evt):
        #NOTE: button4 and button5 will activate the same view
        pub.sendMessage(M_DATA_MODE_CHANGED, data='M')
        self.show_new(3)

    def OnToolbarButton6(self, evt):
        #NOTE: this is just a GUI stuff, no networking activity
        ret = LogManager.login()
        if ret != 0:
            return
        from SimpleDialogs import DialogSync
        dlg = DialogSync()
        ret = dlg.ShowModal()
        dlg.Destroy()
        
        if ret == 0:
            msg = "sync successful"
            icon = wx.ICON_INFORMATION
        else:
            icon = wx.ICON_ERROR
            if ret == -1:
                msg = 'login failed'
            elif ret == -2:
                msg = 'pull failed'
            elif ret == -3:
                msg = 'merge failed:my fault, this is not an network error'
            elif ret == -4:
                msg = 'push failed'
            elif ret == -5:
                msg = 'logout failed: I mean, so what? No big deal'
            elif ret == -6:
                msg = 'timeout:100 seconds has elapsed, find a better network'
            else:
                msg = 'I do not know what the fuck is going on:ret=%d'%(ret)

        dlg=wx.MessageDialog(None,msg,"sync message",icon)
        result=dlg.ShowModal()

        dlg.Destroy()

    def OnToolbarButton7(self, evt):
        from SimpleDialogs import DialogChooseDB
        from DataManager import DataManager
        retl = []
        dlg = DialogChooseDB(retl)
        dlg.ShowModal()
        dlg.Destroy()
        #print(retl[0])
        DataManager.change_db(retl[0])
        self.SetTitle(retl[0])
        
        

    def show_new(self, idx):
        for i in self.views:
            i[0].Hide()
            i[1].Hide()
        self.views[idx][0].Show()
        self.views[idx][1].Show()
        self.sizer_top.Layout()
